package com.chinasoft.news.service.impl;

import com.chinasoft.news.customexception.BussinessException;
import com.chinasoft.news.customexception.SystemException;
import com.chinasoft.news.mapper.UserMapper;
import com.chinasoft.news.pojo.Code;
import com.chinasoft.news.pojo.ExType;
import com.chinasoft.news.pojo.Result;
import com.chinasoft.news.pojo.User;
import com.chinasoft.news.service.UserService;
import com.chinasoft.news.utils.JwtUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    UserMapper userMapper;

    @Override
    public Boolean registerUser(User user) {
        String newPW = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(newPW);
        User user1 = userMapper.selectByName(user.getUsername());
        if (user1 != null) {
            return false;
        }

        int insert = userMapper.insert(user);
        if (insert != 1) {
            throw new SystemException("insert failed " + user, ExType.SystemInsertFail);
        }
        return true;
    }

    @Override
    public Result login(User user) {
        Result result = new Result(0, "login success");
        User user1 = userMapper.selectByName(user.getUsername());
        if(user1==null){
            result.setStatus(1);
            result.setMessage("用户不存在或密码错误！");
            return  result;
        }
        String loginPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(user1.getPassword().equals(loginPassword)){
            String token = JwtUtils.createToken(user1.getId().toString(), user1.getUsername());
            System.out.println(token);
            result.setToken(token);
        }else {
            result.setStatus(1);
            result.setMessage("用户不存在或密码错误！");
        }
        System.out.println(result);

        return result;
    }

    @Override
    public User getUserInfo(String token) {
        String strId = JwtUtils.getAudience(token);
        User user = userMapper.selectById(Integer.parseInt(strId));
        if(user==null){
            throw new BussinessException("user not found"+ strId, ExType.BussRecordNotFound);
        }

        return user;
    }

    @Override
    public User getUserInfo1(Integer userId) {
        User user = userMapper.selectById(userId);
        if(user==null){
            throw new BussinessException("user not found"+ userId, ExType.BussRecordNotFound);
        }

        return user;
    }

    @Override
    public Boolean update(String password,Integer id) {
        int update = userMapper.update(password, id);
        if(update!=1){
            throw new BussinessException("password update fail"+id,ExType.BussPasswordFail);
        }

        return true;
    }

    @Override
    public Boolean updateInfo(String nickname, String email, Integer id) {
        int info = userMapper.updateInfo(nickname, email, id);
        if(info!=1){
            throw new BussinessException("userinfo update fail"+id,ExType.BussUpdateUserInfoFail);
        }
        return true;
    }

    @Override
    public Boolean updateAvatar(String avatar, Integer id) {
        int updateAvatar = userMapper.updateAvatar(avatar, id);
        if(updateAvatar!=1){
            throw  new BussinessException("update avatar fail"+id,ExType.BussUpdateAvatar);
        }
        return true;
    }

}
